//
//  ContactDetailViewController.m
//  BrushFace
//
//  Created by 小小旭 on 15/12/15.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "ContactDetailViewController.h"
#import "DetailInfoCell.h"
#import "DetailDefaultCell.h"
#import "DetailEDUCell.h"
#import "DetailTagCell.h"
#import "NavHelper.h"
#import "ContactViewModel.h"
#import "CommonFriendVC.h"

@interface ContactDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSArray *_dataArray;
    NSDictionary *_currentDict;
    NSArray      *_cityArr;
}
@property(nonatomic,strong)ContactViewModel *reqModel;
@end

@implementation ContactDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navBar setTitle:@"详细资料"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    
    [self dataInit];
    [self UIConfig];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - customMethod
- (void)UIConfig{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, SCREEN_HEIGHT-65) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
}
- (void)dataInit{
    _dataArray = @[@"",@"",@"",@"",@""];
    _reqModel = [ContactViewModel SceneModel];
    [_reqModel fetchFriendDetailInfoWithUserId:self.userid andCB:^(id output) {
        _currentDict = output;
        [_tableView reloadData];
    }];
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"json"];
    NSData *data = [[NSData alloc]initWithContentsOfFile:plistPath];
    id jsondata = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    _cityArr = (NSArray *)jsondata;
    
    
}


#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        DetailInfoCell *cell = [DetailInfoCell loadBundleNib];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HEADERIMAGE_FETCH,_currentDict[@"userid"]]]];
        cell.nickNameLabel.text = _currentDict[@"nickname"];
        
        if ([_currentDict[@"sex"] isNotEmpty])
        {
            if ([_currentDict[@"sex"] integerValue] == 2)
            {
                cell.tagBackImage.image = [UIImage imageNamed:@"90lastWomanTag"];
            }
            
        }
        
        if ([_currentDict[@"age"] isNotEmpty])
        {
            NSString *ageStr = [NSString stringWithFormat:@"%@",_currentDict[@"age"]];
            cell.ageTage.text = [NSString stringWithFormat:@"%2d后",[ageStr intValue]];
        }
        else
        {
           cell.ageTage.text = @"未知";
        }
       
#pragma mark------qiweipeng
        
        if ([[_currentDict objectForKey:@"level"] intValue]==1) {
            cell.revelLabel.text = @"朋友";
        }
        else
        {
            cell.revelLabel.text = @"朋友的朋友";
        }
        
#pragma mark-------------------------
        NSInteger fullnamed = [[NSString stringWithFormat:@"%@",_currentDict[@"fullnamed"]] integerValue];
        if (fullnamed == 1) {
            cell.fullNameLabel.hidden = NO;
        }else{
            cell.fullNameLabel.hidden = YES;
        }
        
        NSString *homeCity;
        NSString *liveCity;
        NSString *homep;
        NSString *liveP;
        
        
        
        for (NSDictionary *subDict in _cityArr) {
            
            if ([_currentDict[@"homeprovince"] isNotEmpty]) {
                
                if ([[subDict objectForKey:@"value"] isEqualToString:_currentDict[@"homeprovince"]])
                {
                    
                    homep = subDict[@"name"];
                    NSArray * homeCityArr = [subDict objectForKey:@"items"];
                    for (NSDictionary *homeCityDict in homeCityArr) {
                        if ([[homeCityDict objectForKey:@"value"] isEqualToString:_currentDict[@"homecity"]]) {
                            homeCity = homeCityDict[@"name"];
                        }
                    }
                }
                
            }else{
                homep = @"未知";
                homeCity = @"未知";
            }
            

            if ([_currentDict[@"liveprovince"] isNotEmpty])
            {

            if ([[subDict objectForKey:@"value"] isEqualToString:_currentDict[@"liveprovince"]]) {
                liveP = subDict[@"name"];
                NSArray * homeCityArr = [subDict objectForKey:@"items"];
                for (NSDictionary *homeCityDict in homeCityArr)
                {
                    if ([[homeCityDict objectForKey:@"value"] isEqualToString:_currentDict[@"livecity"]]) {
                        liveCity = homeCityDict[@"name"];
                    }
                }
            }
            }else{
            liveCity = @"未知";
            liveP = @"未知";
            }
        }

        cell.habitatLabel.text = [NSString stringWithFormat:@"居住地：%@-%@",liveP,liveCity];
        cell.hometownLabel.text = [NSString stringWithFormat:@"家乡：%@-%@",homep,homeCity];
        
        return cell;
    }
    if (indexPath.section == 1) {
        DetailDefaultCell *cell = [DetailDefaultCell loadBundleNib];
        cell.detail.text = [NSString stringWithFormat:@"%@人",_currentDict[@"commonfriendcount"]];
        return cell;
    }
    if (indexPath.section == 2) {
        DetailEDUCell *cell = [DetailEDUCell loadBundleNib];
        cell.borrowinCount.text = [NSString stringWithFormat:@"%@",_currentDict[@"done"]];
        cell.borrowoutCount.text = [NSString stringWithFormat:@"%@",_currentDict[@"lend"]];
        cell.outdateCount.text = [NSString stringWithFormat:@"%@",_currentDict[@"timeout"]];
        cell.creditLabel.text = [NSString stringWithFormat:@"%@",_currentDict[@"totalAmount4view"]];
        return cell;
    }
    if (indexPath.section == 3) {
        DetailTagCell *cell = [DetailTagCell loadBundleNib];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if ([_currentDict[@"companyname"] isNotEmpty]&&[_currentDict[@"profession"] isNotEmpty]&&[_currentDict[@"industry"] isNotEmpty]&&[_currentDict[@"education"] isNotEmpty]&&[_currentDict[@"schoolname"] isNotEmpty]) {
            cell.nullView.hidden = YES;
        }else{
            cell.nullView.hidden = NO;
        }
        return cell;
    }
    if (indexPath.section == 4) {
        DetailDefaultCell *cell = [DetailDefaultCell loadBundleNib];
        cell.title.text  = @"更多";
        cell.detail.hidden = YES;
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==3) {
        return 30;
    }
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 3) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        view.backgroundColor = [UIColor clearColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40, 30)];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = [UIColor lightGrayColor];
        label.text = @"标签";
        [view addSubview:label];
        return view;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 150;
    }
    if (indexPath.section ==2 ) {
        return 130;
    }
    if (indexPath.section == 3) {
        return 80;
    }
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==2) {
        //[NavHelper gotoFriendEDWithFriendID:self.userid];
    }
    if (indexPath.section == 4) {
        [NavHelper gotoFriendMoreVCWithFriendID:_currentDict];
    }
    if (indexPath.section == 1) {
        CommonFriendVC *commonVC = [[CommonFriendVC alloc] init];
        [commonVC setFriendListWithArray:[_currentDict objectForKey:@"commonfriendllist"]];
        commonVC.name = _currentDict[@"nickname"];
        [commonVC setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:commonVC animated:YES];
    }
}


@end
